package com.qf.controller;

import com.qf.dto.UserDto;
import com.qf.entity.User;
import com.qf.result.ResponseResult;
import com.qf.service.UserService;
import com.qf.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(tags = "员工管理", description = "员工管理相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getUserList")
    @ApiOperation(value = "员工列表", notes = "获取所有员工列表")
    public ResponseResult getUserList() {
        return userService.getUserList();
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "添加员工", notes = "添加员工信息")
    public ResponseResult addUser(@Valid @RequestBody UserDto userDto) {
        return userService.addUser(BeanCopyUtils.copyBean(userDto.setId(null), User.class));
    }

    @PutMapping("/updateUser")
    @ApiOperation(value = "更新员工", notes = "更新员工信息")
    public ResponseResult updateUser(@Valid @RequestBody UserDto userDto) {
        return userService.updateUser(BeanCopyUtils.copyBean(userDto, User.class));
    }

    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除员工", notes = "删除员工信息")
    public ResponseResult deleteUser(@PathVariable Integer id) {
        return userService.deleteUser(id);
    }
}
